package tests;

import static org.junit.Assert.*;

import java.util.logging.Logger;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import ownserver.ServerShip;

import pathfinding.GridCell;
import pathfinding.SpaceTimeReservationTable;

import com.sun.sgs.app.AppContext;
import com.sun.sgs.app.ManagedReference;
import com.sun.sgs.auth.Identity;
import com.sun.sgs.kernel.TransactionScheduler;
import com.sun.sgs.test.util.SgsTestNode;
import com.sun.sgs.test.util.TestAbstractKernelRunnable;

public class SpaceTimeReservationTableTest {

	private static SgsTestNode serverNode;
	
    private static TransactionScheduler txnScheduler;
    private static Identity taskOwner;
    
    private static final String NAME = "SpaceTimeReservationTableTest";
    
    private static double errorLimit = 1E-12;
	
    private static final Logger logger =
            Logger.getLogger(SpaceTimeReservationTableTest.class.getName());
	
    
    @BeforeClass
	public static void setUpBeforeClass() throws Exception {
		serverNode = new SgsTestNode(NAME, null, null);
        
        
        txnScheduler = serverNode.getSystemRegistry().
                getComponent(TransactionScheduler.class);
        taskOwner = serverNode.getProxy().getCurrentOwner();
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		serverNode.shutdown(true);
	}
	
    @Test
	public void testConstructor() throws Exception {
    	txnScheduler.runTask(
				new TestAbstractKernelRunnable() {
				public void run() throws Exception {
					SpaceTimeReservationTable table = new SpaceTimeReservationTable();
				}
				}, taskOwner);
	}

	@Test
	public void testReserve() throws Exception {
		txnScheduler.runTask(
				new TestAbstractKernelRunnable() {
				public void run() throws Exception {
					SpaceTimeReservationTable table = new SpaceTimeReservationTable();
					ServerShip s = new ServerShip(0,0,0,0,0,0);
					ManagedReference<ServerShip> reserver = AppContext.getDataManager().createReference(s);
					table.reserve(new GridCell(0,0), 0, 0, reserver);
				}
				}, taskOwner);
	}

	@Test
	public void testRemoveReservationsIntIntManagedReferenceOfServerShip() throws Exception {
		fail("Not yet implemented");
	}

	@Test
	public void testRemoveReservationsManagedReferenceOfServerShip() throws Exception {
		fail("Not yet implemented");
	}

	@Test
	public void testUpdateReservations() throws Exception {
		fail("Not yet implemented");
	}

	@Test
	public void testIsReserved() throws Exception {
		txnScheduler.runTask(
				new TestAbstractKernelRunnable() {
				public void run() throws Exception {
					SpaceTimeReservationTable table = new SpaceTimeReservationTable();
					
					// table is empty, nothing should be reserved
					assertTrue(table.isReserved(new GridCell(0,0), 4, 6) == false);
					
					ServerShip s = new ServerShip(0,0,0,0,0,0);
					ManagedReference<ServerShip> reserver = AppContext.getDataManager().createReference(s);
					table.reserve(new GridCell(0,0), 1, 5, reserver);
					
					assertTrue(table.isReserved(new GridCell(0,0), 4, 6));
					assertTrue(table.isReserved(new GridCell(0,0), 0, 2));
					assertTrue(table.isReserved(new GridCell(0,0), 3, 4));
					
					assertTrue(table.isReserved(new GridCell(0,0), 0, 0) == false);
					assertTrue(table.isReserved(new GridCell(0,0), 6, 7) == false);
				}
				}, taskOwner);
	}

}
